<?php
namespace app\admin\controller;
use app\admin\common\Base;
use think\Db;
class NewsClass extends Base
{
	// 分类列表
    public function index()
    {
    	$newsClass = Db::table('news_class')->where(['is_del' => 1])->select(); //1表示未删除  2表示已删除
		$total = count($newsClass);
    	$this -> view -> assign('newsClass', $newsClass);
    	$this -> view -> assign('total', $total);
    	return $this -> fetch('newsClass/news_class_list');
    }
	// 编辑文章分类
    public function modify_class ()
    {
    	$id = input('param.id');
		$newsIdData = Db::table('news_class')->where('id', $id)->find();
		$this -> view -> assign('data', $newsIdData);
        return $this -> view-> fetch('newsClass/news_class_edit');
    }
	// 更新文章分类信息
	public function update_class ()
    {
    	$param = input('param.');
		$id = $param['id'];
		$sort= $param['sort'];
		$name = $param['name'];
		$res = Db::table('news_class')->where('id', $id)->update(['name' => $name, 'sort'=>$sort]);
		if ($res) {
			return ['success'=> true, 'msg'=>'修改成功'];
		} else {
			return ['success'=> false, 'msg'=>'修改失败'];
		}
    }
	// 删除指定文章分类
	public function delete_class ()
    {
    	$param = input('param.');
		$id = intval($param['id']);
		$res = Db::table('news_class')->where('id',$id)->update(['is_del' => 2]);
		if ($res) {
			return ['success'=> true, 'msg'=>'删除成功'];
		} else {
			return ['success'=> false, 'msg'=>'删除失败'];
		}
    }
	// 文章分类视图
	public function add_class ()
    {
    	return $this->fetch('newsClass/news_class_add');
    }
	// 创建分类
	public function create_class ()
    {
    	$param = input('param.');
		$name = $param['name'];
		$sort = intval($param['sort']);
		$data = ['name' => $name, 'sort' => $sort];
		$res = Db::table('news_class')->insert($data);
		if ($res) {
			return ['success'=> true, 'msg'=>'创建成功'];
		} else {
			return ['success'=> false, 'msg'=>'创建失败'];
		}
    }
	// 文章分类已经删除的分类列表
    public function is_del_class()
    {
        $list = Db::table('news_class')->where("is_del = 2")->order('id desc')->select();
        $total = count($list);
        $this -> view -> assign('total', $total);
        $this -> view -> assign('list', $list);
    	return $this -> fetch('newsClass/news_class_del');
    }
	
	// 恢复文章分类删除
	public function update_is_del_class()
    {
        $param = input('param.');
		$id = intval($param['id']);
		$res = Db::table('news_class')->where('id',$id)->update(['is_del' => 1]);
		if ($res) {
			return ['success'=> true, 'msg'=>'更新成功'];
		} else {
			return ['success'=> false, 'msg'=>'更新失败'];
		}
    }
}
